import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'
import VueCookies from 'vue-cookies'

Vue.config.productionTip = false
// 设置axios的基准url
Vue.axios.defaults.baseURL = 'http://127.0.0.1:8020/'
// 设置用于csrf验证的cookie名和header名，要与django的一致
Vue.axios.defaults.xsrfCookieName = 'csrftoken'
Vue.axios.defaults.xsrfHeaderName = 'X-CSRFToken'
// 设置请求拦截器
Vue.axios.interceptors.request.use(
  config => {
    // 当用户登录时，token存在，将token加入authorization中用于验证
    if (store.state.token) {
      config.headers.Authorization = 'Token' + ' ' + store.state.token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// 使用vue-cookies管理token以及用户状态
Vue.use(VueCookies)

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount('#app')
